/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package consys.social.core.service;

import consys.social.core.NotAuthorizedException;
import consys.social.core.bo.Entity;
import consys.social.core.dao.FollowDao;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author petan
 */
public class FollowServiceImpl implements FollowService {

    private FollowDao followDao;

    public void setFollowDao(FollowDao followDao) {
        this.followDao = followDao;
    }

    @Override
    public List<String> getAllFollowersUid(Entity entity) throws IOException {
        return followDao.getAllFollowers(entity);
    }

    @Override
    public List<String> getAllFollowingsUid(Entity entity) throws IOException {
        return followDao.getAllFollowings(entity);
    }

    @Override
    public List<Entity> getRandomFollowers(Entity entity, int count) throws IOException {
        return followDao.getRandomFollowers(entity, count);
    }

    @Override
    public List<Entity> getRandomFollowings(Entity entity, int count) throws IOException {
        return followDao.getRandomFollowings(entity, count);
    }

    @Override
    public List<Entity> getFollowings(Entity entity, int from, int howMany) throws IOException {
        return followDao.getFollowings(entity, from, howMany);
    }

    @Override
    public List<Entity> getFollowers(Entity entity, int from, int howMany) throws IOException {
        return followDao.getFollowers(entity, from, howMany);
    }

    @Override
    public void follow(Entity who, Entity whom) throws IOException, NotAuthorizedException {
        Entity source = AuthenticatedUser.getUser();
        if (who == null || whom == null) {
            throw new IllegalArgumentException("You have to say who wants follow whom");
        }
        if (!who.equals(source)) {
            throw new NotAuthorizedException("User is performing an action for an other user");
        }
        if (who.equals(whom)) {
            throw new IllegalArgumentException("Entity cannot follow itself");
        }
        if (followDao != null) {
            followDao.follow(who, whom);
        }
    }

    @Override
    public void unfollow(Entity who, Entity whom) throws IOException, NotAuthorizedException {
        Entity source = AuthenticatedUser.getUser();
        if (who == null || whom == null) {
            throw new IllegalArgumentException("You have to say who wants unfollow whom");
        }
        if (!who.equals(source)) {
            throw new NotAuthorizedException("User is performing an action for an other user");
        }
        if (who.equals(whom)) {
            throw new IllegalArgumentException("Entity cannot unfollow itself");
        }
        if (followDao != null) {
            followDao.unfollow(who, whom);
        }
    }

    @Override
    public boolean isFollowing(Entity who, Entity whom) throws IOException {
        if (who == null || whom == null) {
            throw new IllegalArgumentException("who is null or whom is null");
        }
        if (who.equals(whom)) {
            return true;
        }
        return followDao.isFollowing(who, whom);
    }

    @Override
    public void block(Entity who, Entity whom) throws IOException, NotAuthorizedException {
        Entity source = AuthenticatedUser.getUser();
        if (who == null || whom == null) {
            throw new IllegalArgumentException("who is null or whom is null");
        }
        if (!who.equals(source)) {
            throw new NotAuthorizedException("User is performing an action for an other user");
        }
        if (who.equals(whom)) {
            throw new IllegalArgumentException("Entity cannot block himself.");
        }
        followDao.block(who, whom);
    }
}
